package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.thread.WmUserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private MinIoTemplate minIoTemplate;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile)  {

        InputStream inputStream = null;
        try {
            //1.获取文件对象的输入流
            inputStream = multipartFile.getInputStream();
            //2.将图片上传到minio
            //2.1获取图片的后缀,传过来的图片后缀为images/png，需要分割
            String contentType = multipartFile.getContentType();
            if (StringUtils.isEmpty(contentType)){
                log.warn("图片格式有误");
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR);
            }
            //2.2解析图片类型，分割，组成为.png
            String suffixName = "." + contentType.split("/")[1];
            //2.3上传
            String path = minIoTemplate.uploadFile("", System.currentTimeMillis() + suffixName, inputStream, "image");

            //3.将图片地址写入数据库中
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUserId(WmUserThreadLocal.get().getId());
            wmMaterial.setUrl(path);
            wmMaterial.setType((short) 0);
            wmMaterial.setIsCollection((short) 0);
            wmMaterial.setCreatedTime(new Date());

            int insert = wmMaterialMapper.insert(wmMaterial);
            if (insert < 1){
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }

            return ResponseResult.okResult(wmMaterial);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

    }

    @Override
    public ResponseResult getMaterialList(WmMaterialDto dto) {

        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        Page<WmMaterial> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        //判断是否是查询收藏的素材
        Short isCollection = dto.getIsCollection();
        wrapper.eq( isCollection == 1,WmMaterial::getIsCollection,isCollection);
        //查询当前用户
        wrapper.eq(WmMaterial::getUserId,WmUserThreadLocal.get().getId());
        //构造排序条件
        wrapper.orderByDesc(WmMaterial::getCreatedTime);
        page = wmMaterialMapper.selectPage(page, wrapper);

        PageResponseResult pageResponseResult = new PageResponseResult((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }
}
